package
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;
	
	import org.flixel.*;

	public class PlayerRoomState extends FlxState
	{
		private static const TOP_BAR_WIDTH:int = 700;
		private static const TOP_BAR_HEIGHT:int = 100;
		private static const TOP_BAR_X:int = 0;
		private static const TOP_BAR_Y:int = 0;
		
		private static const ACHIEV_BAR_WIDTH:int = 120;
		private static const ACHIEV_BAR_HEIGHT:int = 400;
		private static const ACHIEV_BAR_X:int = 0;
		private static const ACHIEV_BAR_Y:int = 100;
		
		private static const FRIEND_BAR_WIDTH:int = 700;
		private static const FRIEND_BAR_HEIGHT:int = 140;
		public static const FRIEND_BAR_X:int = 0;
		public static const FRIEND_BAR_Y:int = 500;
		
		private static const PBOX_BAR_WIDTH:int = 250;
		private static const PBOX_BAR_HEIGHT:int = 250;
		private static const PBOX_BAR_X:int = 160;
		private static const PBOX_BAR_Y:int = 280;
		
		protected var topBar:FlxSprite;
		
		protected var achievementComponent:PlayerAchievementComponent;
		protected var achievementPopup:FlxGroup;
		
		protected var puzzleBox:FlxSprite;
		protected var puzzleBoxOverlapped:Boolean;
		protected var hoverFilter:GlowFilter;
		
		protected var backdrop:FlxSprite;
		
		protected var puzzleLoader:PuzzleChallengesLoader;
		protected var puzzleLoaderShown:Boolean;
		
		protected var exitButton:FlxSprite;
		
		//buttons
		protected var myPuzzlesButton:FlxSprite;
		protected var quickPlayButton:RoomButton;
		protected var storyModeButton:RoomButton;
		protected var playerPuzzlesButton:RoomButton;
		protected var puzzleEditorButton:RoomButton;
		protected var invitesButton:RoomButton;
		
		//achievements
		protected var achievesTimer:Number;
		protected var achievDisplayed:Boolean;
		protected var achievCurr:AchievementReport;
		
		protected var notification:FlxSprite;
		protected var notified:Boolean;
		protected var musicIcon:Music;

		
		public function PlayerRoomState()
		{
			achievementPopup = new FlxGroup();
			add(new FlxSprite(0,0, Res.bg));
			add(new FlxSprite(0, 90, Res.RoomBG));
			createTitleBar();
			createAchievementsBar();
			createFriendBar();
			createPuzzleBox();
			createButtons();
			musicIcon = new Music(676,0);
			add(musicIcon);
			
			Elemental.Log.startDquest(Logging.PLAYER_ROOM);
		}
		
		public override function update():void {
			super.update();
			musicIcon.updateMusic();
			
			if (puzzleLoaderShown) {
				Elemental.friendBar.active = false;
				if (FlxG.mouse.justPressed() && exitButton.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
					remove(puzzleLoader);
					remove(exitButton);
					remove(backdrop);
					Elemental.friendBar.active = true;
					puzzleLoaderShown = false;
				}
			} else {
				updateAchievementOverlap();
				updatePuzzleBoxOverlap();
				checkButtons();
			}
			updateAchievementDisplay();
		}
		
		/**
		 * handles everything with displaying an achievement in game
		 */
		private function updateAchievementDisplay():void {
			if (!achievDisplayed) {
				if (Elemental.achievements.hasAchievementToReport()) {
					achievCurr = Elemental.achievements.getNextAchievementToReport();
					this.add(achievCurr);
					achievesTimer = 0;
					achievDisplayed = true;
				}
			} else if (achievesTimer > 2.2) {
				this.remove(achievCurr, true);
				if (Elemental.achievements.hasAchievementToReport()) {
					achievCurr = Elemental.achievements.getNextAchievementToReport();
					this.add(achievCurr);
					achievesTimer = 0;
				} else {
					achievDisplayed = false;
					achievesTimer = 0;
				}
			} else {
				achievesTimer += FlxG.elapsed;
			}
		}
		
		private function updateAchievementOverlap():void {
			if (achievementComponent.hasPopup()) {
				this.remove(achievementPopup, true);
				achievementPopup = achievementComponent.getNextPopup();
				this.add(achievementPopup);
			}
			if (!achievementComponent.isStillOverlapping()) {
				this.remove(achievementPopup, true);
			}
		}
		
		private function updatePuzzleBoxOverlap():void {
			if (puzzleBox.overlapsPoint(FlxG.mouse.screenX,FlxG.mouse.screenY)) {
				if (!puzzleBoxOverlapped) {
					puzzleBox.setFilter(hoverFilter);
					puzzleBoxOverlapped = true;
				}
			} else {
				if (puzzleBoxOverlapped) {
					puzzleBox.removeFilter();
					puzzleBoxOverlapped = false;
				}
			}
			
			// open up the mini puzzle loader
			if (FlxG.mouse.justPressed() && puzzleBoxOverlapped) {
				add(backdrop);
				add(puzzleLoader);
				add(exitButton);
				puzzleLoaderShown = true;
			}
		}
		
		protected function createTitleBar():void {
			topBar = new FlxSprite(0, 0, Res.PlayerTitlebar);
			add(topBar);
			
			var title:FlxText = new FlxText(72, 0, 400, "Welcome back!", false);
			title.setFormat("Georgia", 40, 0x000000, "left", 0, true);
			title.y = (topBar.height - title.height) / 2 - 5;
			add(title);
		}
		
		protected function createAchievementsBar():void {
			achievementComponent = new PlayerAchievementComponent();
			this.add(achievementComponent);
		}
		
		private function createFriendBar():void {
			if (!Elemental.friendBar.isInitialized()) {
				Elemental.friendBar.initialize();
			}
			add(Elemental.friendBar);
		}

		private function createPuzzleBox():void {
			puzzleBox = new FlxSprite(PBOX_BAR_X, PBOX_BAR_Y, Res.GiftBox);
			add(puzzleBox);
			
			puzzleBoxOverlapped = false;
			hoverFilter = new GlowFilter(0xFFFFFF, 0.8, 15, 15, 3);
			
			puzzleLoader = new PuzzleChallengesLoader((FlxG.width - MiniPuzzleLoader.CONTAINER_WIDTH)/2, (FlxG.height - MiniPuzzleLoader.CONTAINER_HEIGHT)/2);
			backdrop = new FlxSprite(0, 0, Res.Backdrop);
			exitButton = new FlxSprite(0, 0, Res.ExitButton);
			exitButton.x = puzzleLoader.x + PuzzleChallengesLoader.CONTAINER_WIDTH - exitButton.width;
			exitButton.y = puzzleLoader.y - exitButton.height - 5;
			
			Client.loadAllPuzzleChallenges(Elemental.fbInfo.userID, loadData);
			puzzleLoaderShown = false;
		}
		
		/**
		 * Called when our client has received data from the server.
		 * Removes loading graphic, creates a listing for five puzzles.
		 */
		private function loadData(e:Event):void {
			var maps:String = e.target.data;
			trace("returned " + e.target.data);
			
			if (maps != "no maps") {
				var mapArray:Array = maps.split("/");
				var len:int = mapArray.length;
				notification = new FlxSprite(275,230,Res.notificationBubble);
				var text:FlxText = new FlxText(notification.x,notification.y,notification.width,new String(len), false);
				text.setFormat("Georgia", 48, 0xAA0000, "center", 0, true);
				text.y = notification.y + (65 - text.height)/2;
				this.add(notification);
				this.add(text);
			}
			notified = true;
		}
		
		private function createButtons():void {
			myPuzzlesButton = new FlxSprite(0, 0, Res.MyPuzzlesButton);
			myPuzzlesButton.x = FlxG.width - myPuzzlesButton.width - 20;
			myPuzzlesButton.y = (topBar.height - myPuzzlesButton.height) / 2 - 3;
			add(myPuzzlesButton);

			quickPlayButton = new RoomButton("Quick Play", 425, 140, function ():void {
				FlxG.switchState(new QuickPlayState());
			});
			add(quickPlayButton);
			
			storyModeButton = new RoomButton("Play Story Mode", 425, 205, function ():void {
				FlxG.switchState(new MenuState());
			});
			add(storyModeButton);
			
			playerPuzzlesButton = new RoomButton("View All Puzzles", 425, 270, function ():void {
				FlxG.switchState(new LoadPuzzlesState());
			});
			add(playerPuzzlesButton);
			
			puzzleEditorButton = new RoomButton("Create Puzzles", 425, 335, function ():void {
				FlxG.switchState(new MapEditorState(null));
			});
			add(puzzleEditorButton);
			
			invitesButton = new RoomButton("Invite Friends", 425, 400, function ():void {
				Elemental.Log.logInvites();
				Client.updatePlayerStat(Elemental.fbInfo.userID, "invites", 1);
				var myDelay:Timer = new Timer(1000);
				myDelay.addEventListener(TimerEvent.TIMER, function f():void{
					trace("sent invite request!");
					Elemental.fbInfo.sentRequest(Elemental.fbInfo.fullName+" invites you to play Poofed! Join now to play and create puzzles, and challenge your friends to play them!");
				});
				myDelay.start();
			});
			add(invitesButton);
		}
		
		private function checkButtons():void {
			if (FlxG.mouse.justPressed() && myPuzzlesButton.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				FlxG.switchState(new MyPuzzles());
			}
		}
		
		
		/**
		 * Especially remove the friendbar because FlxG.switchState will destroy everything
		 * that's currently added to the screen.
		 */
		public override function destroy():void {
			remove(Elemental.friendBar, true);
			super.destroy();
		}
	}
}